#!/bin/sh
# PCP QA Test No. 1197
# pmlogextract handling of empty archives
#
# Copyright (c) 2020 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

if which xz >/dev/null
then
    :
else
    _notrun "No xz(1) executable"
    # NOTREACHED
fi

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

mkdir $tmp

# really empty
#
for suff in 0 meta index
do
    echo >$tmp/0.$suff
done

# just the label record, and an OK archive
#
for suff in 0 meta index
do
    if [ "$PCP_ARCHIVE_VERSION" = 3 ]
    then
	labelsize=808
    else
	labelsize=132
    fi
    dd if=tmparch/foo.$suff of=$tmp/1.$suff ibs=1 count=$labelsize >/dev/null 2>&1
    cp tmparch/foo.$suff $tmp/2.$suff
done

# and now compressed versions of the .meta and .0 files
#
for s in 0 1
do
    d=`expr $s + 3`
    cp $tmp/$s.index $tmp/$d.index
    for suff in 0 meta
    do
	cp $tmp/$s.$suff $tmp/$d.$suff
	xz $tmp/$d.$suff
    done
done

ls -l $tmp >>$seq_full

# real QA test starts here
cd $tmp
pmlogextract 0 1 2 3 4 $tmp
if [ ! -f $tmp.0 ]
then
    echo "Botch, no output file from pmlogextract!"
    exit
fi
cd $here

pmdumplog -a tmparch/foo | sed -e '/PID for pmlogger:/d' >$tmp.ref
echo "=== foo:tmp expect no diffs ==="
pmdumplog -a $tmp.0 | sed -e '/PID for pmlogger:/d' >$tmp.xtract
if diff $tmp.ref $tmp.xtract
then
    :
else
    diff -u  $tmp.ref $tmp.xtract >>$seq_full
    ls -l tmparch/foo.* $tmp.* >>$seq_full
    echo "=== metadata for .ref ===" >>$seq_full
    src/scanmeta -a tmparch/foo.meta 2>&1 | tee -a $seq_full >$tmp.ref.meta
    echo "=== metadata for .xtract ===" >>$seq_full
    src/scanmeta -a $tmp.meta 2>&1 | tee -a $seq_full >$tmp.xtract.meta
    echo "=== diffs ===" >>$seq_full
    diff -u  $tmp.ref.meta $tmp.xtract.meta >>$seq_full
fi

echo
echo "=== only empty archive(s) cases ==="
cd $tmp
pmlogextract 0 $tmp
pmlogextract 0 1 $tmp
pmlogextract 0 1 3 $tmp
pmlogextract 0 1 3 4 $tmp
cd $here

# success, all done
status=0
exit
